<?php

function logError($message){
	if(isset($_SESSION['ERRORMESSAGE'])){
		$_SESSION['ERRORMESSAGE'] .= $message ."<br/>";
	}else{
		$_SESSION['ERRORMESSAGE'] = $message ."<br/>";
	}  	
}

function logSuccess($message){
	if(isset($_SESSION['SUCCESSMESSAGE'])){
		$_SESSION['SUCCESSMESSAGE'] .= $message ."<br/>";
	}else{
		$_SESSION['SUCCESSMESSAGE'] = $message ."<br/>";
	}
}

function logException($e){
	if(file_exists(LOGFILE) && filesize (LOGFILE) > LOGFILEMAXSIZE){
		rename(LOGFILE, LOGFILE . date("j-n-Y-H-i-s"));				
	}
	$fh = fopen(LOGFILE, 'a');	
	$error =  "date: ". date("j/n/Y  H:i:s") . "\n" .
						"code: ".$e->getCode() . "\n" .
						"file: ".$e->getFile() . "\n" .
						"line: ".$e->getLine() . "\n" .
						"message: ".$e->getMessage() . "\n" .
						"trace: ".$e->getTraceAsString() . "\n\n";
	fwrite($fh, $error);		
	fclose($fh);	
	if(DEBUG){
		echo str_replace("\n", "<br/>", $error);
	}
	
}

function handleError($errno, $errstr, $errfile, $errline, array $errcontext)
{
    // error was suppressed with the @-operator
    if (0 === error_reporting()) {
        return false;
    }

    logException(new ErrorException($errstr, 0, $errno, $errfile, $errline));
}
set_error_handler('handleError');

?>